#!/usr/bin/env python

import gtk
import gettext
import os
from mxit import handles
from blend import config
gettext.install('blendIT','locale');

class signals:

   def __init__(self,blend):
      self.blend = blend;

   def on_cancel(self,widget):
      self.blend.gui.editAccountWindow.hide();
      self.blend.gui.editAccountWindow.destroy();

   def on_save(self,widget):
      window = self.blend.gui.editAccountWindow;

      bd = window.entries['dob'].get_date();
      bdate = "%s-%s-%s" % (bd[0],bd[1]+1,bd[2]);

      if window.entries['private'][0].get_active():
         pvt = handles.PrivateNumber.YES;
      else:
         pvt = handles.PrivateNumber.NO;

      if window.entries['gender'][0].get_active():
         gend = handles.Gender.MALE;
      else:
         gend = handles.Gender.FEMALE;
      
      if window.entries['rememberPin'].get_active():
         self.blend.currentAccount.rememberPin = True;
      else:
         self.blend.currentAccount.rememberPin = False;

      if window.entries['autoLogin'].get_active():
         self.blend.currentAccount.autologin = True;
      else:
         self.blend.currentAccount.autologin = False;


      self.blend.conn.update_profile(window.entries['pin'].get_text(),
                                     name=window.entries['name'].get_text(),
                                     birthdate=bdate,
                                     private=pvt,
                                     gender=gend);

      self.blend.currentAccount.password = window.entries['pin'].get_text();

      c = config.config();
      c.save(self.blend.config,self.blend.currentAccount);
      window.destroy();

   def checkPin(self,widget):
      window = self.blend.gui.editAccountWindow;
      if len(window.entries['pin'].get_text()) > 0:
         try:
            d = int(window.entries['pin'].get_text());
            window.pinInfoLabel.set_markup(_('Pin must be more than 6 digits'));
         except ValueError:
            err = _("<span foreground='#FF0000'>Pin must only contain numbers</span>");
            window.pinInfoLabel.set_markup(err);
            met = False;
      else:
         window.pinInfoLabel.set_markup(_('Pin must be more than 6 digits'));

      if len(window.entries['pin'].get_text()) < 6:
         met = False;
      else:
         window.pinInfoLabel.set_markup('');


   def checkDate(self,widget):
      d = widget.get_date();
      err = _("<span foreground='#FF0000'>Cannot select a future date</span>");

      window = self.blend.gui.editAccountWindow;

      if d[0] > time.localtime().tm_year:
         # fail
         window.calInfoLabel.set_markup(err);
         self.dateSelected = False;
         return False;
      elif d[1]+1 > time.localtime().tm_mon:
         # fail
         window.calInfoLabel.set_markup(err);
         self.dateSelected = False;
         return False;
      elif d[2] > time.localtime().tm_mday:
         # fail
         window.calInfoLabel.set_markup(err);
         self.dateSelected = False;
         return False;
      else:
         window.calInfoLabel.set_markup('');
         self.dateSelected = True;
         return True;
